# Gitpod file reference
# https://www.gitpod.io/docs/configure/workspaces/tasks

image: registry.gitlab.com/gitlab-org/gitlab-development-kit/gitpod-workspace:stable
checkoutLocation: gitlab-development-kit/gitlab

tasks:

  - name: GDK
    # "command:" emits gitpod-start
    before: |
      START_UNIXTIME="$(date +%s)"
      echo START_UNIXTIME="$(date +%s)" > /workspace/gitpod_start_time.sh
    command: |
      # send signal to other tasks that Gitpod started
      gp sync-done gitpod-start
      echo "Waiting for other task to copy GDK.."
      gp sync-await gdk-copied && cd /workspace/gitlab-development-kit && gdk help

  - name: GitLab
    # "command:" emits gdk-copied
    init: |
      (
        set -e
        echo "$(date) – Copying GDK" | tee -a /workspace/startup.log
        cp -r $HOME/gitlab-development-kit /workspace/
        cd /workspace/gitlab-development-kit
        mv -v /workspace/gitlab-development-kit/secrets.yml /workspace/gitlab-development-kit/gitlab/config
        # ensure gdk.yml has correct instance settings
        gdk config set gitlab.rails.port 443  |& tee -a /workspace/startup.log
        gdk config set gitlab.rails.https.enabled true  |& tee -a /workspace/startup.log
        gdk config set webpack.host 127.0.0.1  |& tee -a /workspace/startup.log
        gdk config set webpack.static false  |& tee -a /workspace/startup.log
        gdk config set webpack.live_reload false  |& tee -a /workspace/startup.log
        # reconfigure GDK
        echo "$(date) – Reconfiguring GDK" | tee -a /workspace/startup.log
        gdk reconfigure
        # run DB migrations
        echo "$(date) – Running DB migrations" | tee -a /workspace/startup.log
        make gitlab-db-migrate
        # stop GDK
        echo "$(date) – Stopping GDK" | tee -a /workspace/startup.log
        gdk stop
        echo "$(date) – GDK stopped" | tee -a /workspace/startup.log
      )
    command: |
      (
        set -e
        gp sync-done gdk-copied
        gp sync-await gitpod-start
        [[ -f /workspace/gitpod_start_time.sh ]] && source /workspace/gitpod_start_time.sh
        SECONDS=0
        cd /workspace/gitlab-development-kit
        # update GDK
        echo "$(date) – Updating GDK" | tee -a /workspace/startup.log
        export DEFAULT_BRANCH=$(git --git-dir=gitlab/.git branch --show-current)
        gdk config set gitlab.default_branch "$DEFAULT_BRANCH"
        gdk update
        # ensure gdk.yml has correct instance settings
        gdk config set gitlab.rails.hostname $(gp url 3000 | sed -e 's+^http[s]*://++')
        gdk config set gitlab.rails.port 443
        gdk config set gitlab.rails.https.enabled true
        gdk config set webpack.host 127.0.0.1
        gdk config set webpack.static false
        gdk config set webpack.live_reload false
        # reconfigure GDK
        echo "$(date) – Reconfiguring GDK" | tee -a /workspace/startup.log
        gdk reconfigure
        # start GDK
        echo "$(date) – Starting GDK" | tee -a /workspace/startup.log
        export DEV_SERVER_PUBLIC_ADDR=$(gp url 3808)
        export RAILS_HOSTS=$(gp url 3000 | sed -e 's+^http[s]*://++')
        gdk start
        # Run DB migrations
        if [ "$GITLAB_RUN_DB_MIGRATIONS" == true ]; then
          make gitlab-db-migrate
        fi
        cd /workspace/gitlab-development-kit/gitlab
        echo "--- on branch: $DEFAULT_BRANCH"
        echo "--- installing lefthook"
        bundle exec lefthook install
        echo "--- resetting db/structure.sql"
        git checkout db/structure.sql
        echo "--- waiting for GitLab"
        gp ports await 3000
        printf "Awaiting /-/readiness on $(gp url 3000) ..."
        # Check /-/readiness which returns JSON, but we're only interested in the exit code
        #
        # We use http://localhost:3000 instead of the public hostname because
        # it's no longer possible to access as specific cookies are required
        until curl --silent --no-buffer --fail http://localhost:3000/-/readiness > /dev/null 2>&1; do printf '.'; sleep 5; done && echo ""
        # Give Gitpod a few more seconds to set up everything ...
        sleep 5
        printf "$(date) – GitLab is up (took ~%.1f minutes)\n" "$((10*$SECONDS/60))e-1" | tee -a /workspace/startup.log
        gp preview $(gp url 3000) --external || true
        PREBUILD_LOG=(/workspace/.gitpod/prebuild-log-*)
        [[ -f /workspace/gitpod_start_time.sh ]] && printf "Took %.1f minutes from https://gitlab.com/gitlab-org/gitlab/-/blob/master/.gitpod.yml being executed through to completion %s\n" "$((10*(($(date +%s)-${START_UNIXTIME}))/60))e-1" "$([[ -f "$PREBUILD_LOG" ]] && echo "With Prebuilds")"
      )

ports:
  - port: 2222  # sshd
    onOpen: ignore
  - port: 3000  # rails-web
    onOpen: notify
  - port: 3005  # gitlab-docs
    onOpen: notify
  - port: 3010  # gitlab-pages
    onOpen: ignore
  - port: 3808  # webpack
    onOpen: ignore
  - port: 5000  # auto_devops
    onOpen: ignore
  - port: 5778  # jaeger
    onOpen: ignore
  - port: 9000  # object_store / minio
    onOpen: ignore
  - port: 9122  # gitlab-shell
    onOpen: ignore

vscode:
  extensions:
    - GitLab.gitlab-workflow
    - rebornix.ruby@0.28.1
    - wingrunr21.vscode-ruby@0.28.0
    - karunamurti.haml@1.4.1
    - octref.vetur@0.37.3
    - dbaeumer.vscode-eslint@2.4.0
    - DavidAnson.vscode-markdownlint@0.49.0
    - esbenp.prettier-vscode
